﻿

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;


namespace AUI
{
    public class tbl_Customers_Info : Standard_Info
    {
        public int CustomerId { get; set; }
        public int BranchId { get; set; }
        public string CustomerCode { get; set; }
        public string CustomerName { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public string Fax { get; set; }
        public DateTime CreateDate { get; set; }
        public string CreateUser { get; set; }
        public DateTime LastUpdate { get; set; }
        public string LastUser { get; set; }
    }
    public class tbl_Customers_Length
    {
        public static int CustomerCode = 50;
        public static int CustomerName = 256;
        public static int Address = 256;
        public static int Email = 50;
        public static int Phone = 50;
        public static int Mobile = 50;
        public static int Fax = 50;
    }
    public class tbl_Customers_Controller
    {
        #region "Contructor"

        private static tbl_Customers_Controller _instance;

        protected tbl_Customers_Controller() { }

        protected void Dispose()
        {
            _instance = null;
        }

        private static object _syncLock = new object();

        public static tbl_Customers_Controller Instance()
        {
            if (_instance == null)
            {
                lock (_syncLock)
                {
                    if (_instance == null)
                        _instance = new tbl_Customers_Controller();
                }
            }
            return _instance;
        }

        #endregion

        public DataTable GetList(out int ItemCount, string orderBy, string orderDirection, int currentPage, int pageSize, string keyWord, int BranchId)
        {
            DataTable retVal = null;
            ItemCount = 0;
            DataSet ds = DataConnect.ExecuteDataset("prc_Customers_GetList", orderBy, orderDirection, currentPage, pageSize, keyWord, BranchId);
            if (ds != null)
            {
                retVal = ds.Tables[0];
                if (ds.Tables[1] != null)
                    ItemCount = Globals.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            return retVal;
        }


        public DataTable GetAll()
        {
            return DataConnect.ExecuteDatatable("prc_Customers_GetAll");
        }
        public DataTable GetAllByBranch(object keyBranch)
        {
            if (keyBranch.GetType() == typeof(int))
                return DataConnect.ExecuteDatatable("prc_Customers_GetAllByBranchID", keyBranch);
            else
                return DataConnect.ExecuteDatatable("prc_Customers_GetAllByBranchCode", keyBranch);
        }
        public bool Delete(int CustomerId)
        {
            return (Globals.ToInt32(DataConnect.ExecuteNonQuery("prc_Customers_Delete", CustomerId)) > 0);
        }

        public bool IsExist(tbl_Customers_Info i)
        {
            return (Globals.ToInt32(DataConnect.ExecuteScalar("prc_Customers_Exists", i.BranchId, i.CustomerId, i.CustomerCode)) > 0);

        }

        public int Save(tbl_Customers_Info i)
        {
            int retVal = 0;

            if (i.CustomerId == 0)
                retVal = Globals.ToInt32(DataConnect.ExecuteScalar("prc_Customers_Insert", i.BranchId, i.CustomerCode,
                i.CustomerName,
                i.Address,
                i.Email,
                i.Phone,
                i.Mobile,
                i.Fax,
                i.CreateUser));
            else
            {
                if (DataConnect.ExecuteNonQuery("prc_Customers_Update", i.CustomerId,
                    i.BranchId,
                    i.CustomerCode,
                    i.CustomerName,
                    i.Address,
                    i.Email,
                    i.Phone,
                    i.Mobile,
                    i.Fax,
                    i.LastUser) > 0)

                    retVal = i.CustomerId;
            }
            return retVal;
        }

        public tbl_Customers_Info GetInfo(int CustomerId)
        {
            tbl_Customers_Info retVal = null;
            DataTable dt = DataConnect.ExecuteDatatable("prc_Customers_GetInfo", CustomerId);
            if (dt != null)
                foreach (DataRow dr in dt.Rows)
                {
                    retVal = new tbl_Customers_Info();
                    retVal.FillDataProperty(dr);
                }
            return retVal;
        }
    }
}
